package com.chushouya.manager.service.common.impl;

import com.general.framework.core.lang.Beans;
import com.general.framework.core.lang.Dates;
import com.general.framework.core.lang.Numbers;
import com.general.framework.core.lang.Validate;
import com.chushouya.common.support.Contexts;
import com.chushouya.manager.dao.entity.UserPaymentMethodEntity;
import com.chushouya.manager.dao.repository.UserPaymentMethodRepository;
import com.chushouya.manager.dto.admin.payment.UserPaymentMethodApiDTO;
import com.chushouya.manager.dto.admin.payment.UserPaymentMethodSaveDTO;
import com.chushouya.manager.service.common.UserPaymentMethodApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Slf4j
public class UserPaymentMethodApiServiceImpl implements UserPaymentMethodApiService {

    @Resource
    private UserPaymentMethodRepository userPaymentMethodRepository;

    private UserPaymentMethodEntity selectByPrimaryKey(Long id) {
        Validate.isNull(id, "收款方式ID不能为空");
        UserPaymentMethodEntity entity = userPaymentMethodRepository.selectByPrimaryKey(id);
        Validate.isNull(entity, "收款方式不存在");
        return entity;
    }

    @Override
    public UserPaymentMethodApiDTO selectByUserId(Long userId) {
        Validate.isNull(userId, "用户ID不能为空");
        return Beans.copy(
                userPaymentMethodRepository.selectByUserId(userId),
                UserPaymentMethodApiDTO.class
        );
    }

    @Override
    public UserPaymentMethodApiDTO selectById(Long paymentMethodId) {
        return Beans.copy(
                userPaymentMethodRepository.selectByPrimaryKey(paymentMethodId),
                UserPaymentMethodApiDTO.class
        );
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long create(UserPaymentMethodSaveDTO saveDTO) {
        UserPaymentMethodEntity entity = Beans.copy(saveDTO, UserPaymentMethodEntity.class);
        entity.setCreateTime(Dates.getTimeNow());
        entity.setUpdateTime(Dates.getTimeNow());
        entity.setCreateBy(Contexts.getUserContext().getUserName());
        entity.setUpdateBy(Contexts.getUserContext().getUserName());
        entity.setDelFlag(Numbers.INTEGER_ZERO);
        userPaymentMethodRepository.insertSelective(entity);
        
        return entity.getPaymentMethodId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(UserPaymentMethodSaveDTO saveDTO) {
        Validate.isNull(saveDTO, "收款方式信息不能为空");
        Validate.isNull(saveDTO.getPaymentMethodId(), "收款方式ID不能为空");
        
        UserPaymentMethodEntity entity = selectByPrimaryKey(saveDTO.getPaymentMethodId());
        // 只更新有值的字段
        Beans.copyProperties(saveDTO, entity);
        entity.setUpdateTime(Dates.getTimeNow());
        entity.setUpdateBy(Contexts.getUserContext().getUserName());
        
        userPaymentMethodRepository.updateByPrimaryKeySelective(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createUserPayment(Long userId, String accountName, String accountNumber) {
//        Validate.isNull(userId, "用户ID不能为空");
//        Validate.isBlank(accountName, "账户名称不能为空");
//        Validate.isBlank(accountNumber, "账户号码不能为空");
        if (userId == null || accountName == null || accountName.isEmpty() || accountNumber == null || accountNumber.isEmpty()) {
            log.warn("用户ID或账户名称或账户号码为空，跳过操作");
            return;
        }
        // 查询用户是否已存在收款方式
        UserPaymentMethodEntity existingEntity = userPaymentMethodRepository.selectByUserId(userId);
        
        if (existingEntity != null) {
            // 存在则更新
            log.info("用户{}已存在收款方式，进行更新操作", userId);
            existingEntity.setAccountName(accountName);
            existingEntity.setAccountNumber(accountNumber);
            existingEntity.setUpdateTime(Dates.getTimeNow());
            existingEntity.setUpdateBy(Contexts.getUserContext().getUserName());
            userPaymentMethodRepository.updateByPrimaryKeySelective(existingEntity);
        } else {
            // 不存在则新建
            log.info("用户{}不存在收款方式，进行新建操作", userId);
            UserPaymentMethodEntity entity = new UserPaymentMethodEntity();
            entity.setUserId(userId);
            entity.setAccountName(accountName);
            entity.setAccountNumber(accountNumber);
            entity.setCreateTime(Dates.getTimeNow());
            entity.setUpdateTime(Dates.getTimeNow());
            entity.setCreateBy(Contexts.getUserContext().getUserName());
            entity.setUpdateBy(Contexts.getUserContext().getUserName());
            entity.setDelFlag(Numbers.INTEGER_ZERO);
            entity.setPaymentType(Numbers.INTEGER_ZERO);
            userPaymentMethodRepository.insertSelective(entity);
        }
    }
}
